package com.dingyh.shopwebbackground.controller;


import com.dingyh.shopwebbackground.entity.Banned;
import com.dingyh.shopwebbackground.entity.User;
import com.dingyh.shopwebbackground.service.BannedService;
import com.dingyh.shopwebbackground.service.UserService;
import com.dingyh.shopwebbackground.util.CommonResult;
import com.dingyh.shopwebbackground.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dingyuhan
 * @since 2023-02-16
 */
@RestController
@RequestMapping("/banned")
@CrossOrigin
public class BannedController {

    @Autowired
    BannedService bannedService;

    @Autowired
    private UserService userService;

    /**
     * 返回整个禁言表
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/allList")
    public CommonResult<Object> allList(@RequestParam(defaultValue = "1",required = true )int pageIndex, @RequestParam(defaultValue = "10",required = true )int pageSize){
        PageHelper.startPage(pageIndex, pageSize);//初始化分页
        Map<String,Object> map = new HashMap<>();
        List<Banned> banneds = bannedService.allList(map);
        PageInfo<Banned> page = new PageInfo<>(banneds);
        return new CommonResult(200,"",page);
    }

    @RequestMapping("/selectByUseId")
    public CommonResult<Object> selectByUseId(String userid,
                                           @RequestParam(defaultValue = "1",required = true )int pageIndex, @RequestParam(defaultValue = "10",required = true )int pageSize){
        PageHelper.startPage(pageIndex, pageSize);//初始化分页
        Map<String,Object> map = new HashMap<>();
        map.put("userid",userid);
        List<Banned> banneds = bannedService.selectByUseId(map);
        PageInfo<Banned> page = new PageInfo<>(banneds);
        return new CommonResult(200,"",page);
    }

    @RequestMapping("/addBan")
    public CommonResult<Object> addBan(String userid, String reason, String disable) {
        User user = userService.getById(userid);
        if (null == user) {
            return new CommonResult<>(500, "该用户不存在");
        }
        if (!user.getDisable().equals("0")) {
            return new CommonResult(500, "当前用户已有禁言操作！", null);
        }

        return bannedService.sveBanned(userid, reason, disable);
    }
}

